<!--
 * @Author: DUST
 * @Date: 2023-05-17 17:44:02
 * @LastEditors: DUST
 * @LastEditTime: 2023-05-31 13:53:21
 * @FilePath: \vite-project\src\views\admin\components\addTagModal.vue
 * @Description: 
 * undefined
-->
<template>
  <ant-modal
    :visible="visible"
    :type="type"
    :loading="loading"
    :width="400"
    :title="title"
    @confirm="doConfirm"
    @cancel="cancel"
  >
    <a-form
      ref="formref"
      :model="formModel"
      class="ModalForm"
      :labelCol="{ span: 6 }"
      :wrapperCol="{ span: 16 }"
    >
      <a-form-item
        label="标签名称"
        :rules="[{ required: true, message: '标签名称不能为空' }]"
        name="name"
      >
        <a-input placeholder="请输入标签名称" v-model:value="formModel.name" />
      </a-form-item>
      <a-form-item
        label="权重"
        name="sortNum"
      >
        <a-input-number min="0" max="9999" class="inputWidth" placeholder="请输入权重值" v-model:value="formModel.sortNum" />
      </a-form-item>
    </a-form>
  </ant-modal>
</template>

<script setup lang="ts">
import { ref, watch, reactive } from "vue";
import { modalType } from "../../../interface/enum";
import useForm from "@/utils/useForm";

const props = defineProps({
  width: {
    type: Number,
    default: () => 400,
  },
  visible: {
    type: Boolean,
    default: () => false,
  },
  loading: {
    type: Boolean,
    default: () => false,
  },
  type: {
    type: String,
    default: () => "",
  },
  data: {
    type: Object,
    default: () => ({}),
  },
});
const emit = defineEmits(["ok", "update:visible"]);

const title = ref<string>();
watch(
  () => props.type,
  () => {
    console.log(props.type);
    if (props.type == modalType.ADD) title.value = "新增";
    else if (props.type == modalType.EDIT) title.value = "编辑";
  },
  {
    deep: true,
    immediate: true,
  }
);

const formObj = reactive({
  name: undefined,
  sortNum: 0
});

const { formref, formModel } = useForm(formObj);

watch(
  () => props.data,
  () => {
    console.log(props.data);
    if (props.type == modalType.EDIT) formModel.value = { ...formModel.value, ...props.data } ;
  },
  {
    deep: true,
    immediate: true,
  }
);
const cancel = () => {
  emit("update:visible");
}
const doConfirm = () => {
  console.log("进入提交");
  formref.value.validate().then(() => {
    const obj = {
      ...formModel.value
    };
    console.log("要提交的数据：", obj);
    emit("ok", { type: props.type, obj});
  })
}
</script>

<style>
.inputWidth{
  width: 234px;
}
</style>